<?php
  class Config extends Singleton {

    private $config = array();

    public static function load($file = "default"){

      $me = static::getInstance();
      $here = dirname(__FILE__)."/";
      
      if(file_exists($here.$file)){
        if(is_file($here.$file)){
          $me->load_file($here.$file);
        } elseif(is_dir($here.$file) && file_exists($here.$file."/config.php")) {
          $me->load_file($here.$file."/config.php");
        } else {
          throw new Exception("No se pudo cargar la configuración: ".$here.$file);
        }
      }
    }

    public static function get($var, $default = null){
      $me = static::getInstance();

      if(isset($me->config[$var]))
      {
        return $me->config[$var];
      } else {
        // TODO:buscar en la base de datos
        return $default;
      }

    }

    public static function set($var, $val){
      $me = static::getInstance();
      $me->config[$var] = $val;
    }

    public static function setDefault($var, $val){
      $me = static::getInstance();
      if(!isset($me->config[$var])) $me->config[$var] = $val;
    }

    private function load_file($file){
      $config = array();
      require($file);
      $me = static::getInstance();
      $me->config = array_merge($me->config, $config);
    }
    
  }